package handler

import (
	"api/internal/dao"
	"api/internal/model"
	"api/video_api/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 上传视频
func CreateVideo(c *gin.Context) {
	var req request.CreateVideo
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	video := model.Video{
		Title:  req.Title,
		Info:   req.Info,
		Video:  req.Video,
		UserId: req.UserId,
		Status: req.Status,
	}
	if !dao.Create(&video) {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": video,
	})
}

// 列表
func FindVideo(c *gin.Context) {
	var video []model.Video
	dao.GetListByFields(&model.Video{}, &video)
	var videos []*model.Video
	for _, v := range video {
		videos = append(videos, &model.Video{
			Title:  v.Title,
			Info:   v.Info,
			Video:  v.Video,
			UserId: v.UserId,
			Status: v.Status,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": videos,
	})

}

// 删除
func DeleteVideo(c *gin.Context) {
	var req request.DeleteVideo
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": dao.Delete(&model.Video{
			Id: req.Id,
		}),
	})
}
